package com.itany.netclass.service.impl;

import com.itany.netclass.constant.CourseTypeConstant;
import com.itany.netclass.dao.CourseMapper;
import com.itany.netclass.dao.CourseTypeMapper;
import com.itany.netclass.entity.*;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CourseService;
import com.itany.netclass.service.CourseTypeService;
import com.itany.netclass.util.ParameterUtil;

import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-14 上午9:10
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class CourseTypeServiceImpl implements CourseTypeService {
    @Override
    public List<CourseType> findAllCourseType(User user, String parentId) throws NoLoginException {
        CourseTypeMapper courseTypeMapper= (CourseTypeMapper) ObjectFactory.getObject("CourseTypeMapper");
        if(user==null){
            throw new NoLoginException("未登录，请先登录");
        }
        CourseTypeExample example=new CourseTypeExample();
        if(parentId==null){
            parentId=CourseTypeConstant.COURSETYPE_FATHER;
        }
        example.or()
                .andParentIdEqualTo(Integer.parseInt(parentId));
        List<CourseType> list = courseTypeMapper.selectByExample(example);
        return list;
    }

    @Override
    public CourseType findCourseTypeById(String id) {
        CourseTypeMapper courseTypeMapper= (CourseTypeMapper) ObjectFactory.getObject("CourseTypeMapper");
        CourseType courseType = courseTypeMapper.selectByPrimaryKey(Integer.parseInt(id));
        return courseType;
    }

    @Override
    public void addCourseType(String typeName, String parentId) throws NullSringException, TypeExistException {
        CourseTypeMapper courseTypeMapper= (CourseTypeMapper) ObjectFactory.getObject("CourseTypeMapper");
        if(ParameterUtil.isNull(typeName) || ParameterUtil.isNull(parentId)){
            throw new NullSringException("请求参数异常");
        }
        CourseTypeExample example =new CourseTypeExample();
        example.or()
                .andTypeNameEqualTo(typeName)
                .andParentIdEqualTo(Integer.parseInt(parentId));
        List<CourseType> list = courseTypeMapper.selectByExample(example);
        if(!list.isEmpty()){
            throw new TypeExistException("已存在同名类型，无法添加");
        }
        CourseType type1 = courseTypeMapper.selectByPrimaryKey(Integer.parseInt(parentId));
        CourseType type = new CourseType();
        type.setParentId(Integer.parseInt(parentId));
        type.setTypeName(typeName);
        type.setStatus(type1.getStatus());//初始类型和父类型相同
        courseTypeMapper.insertSelective(type);
    }

    @Override
    public List<CourseType> backCourseType(String id) throws NoFatherException {
        if(id.equals(CourseTypeConstant.COURSETYPE_STATUS_QIYONG)){
            throw new NoFatherException("没有父类型无法返回");
        }
        CourseTypeMapper courseTypeMapper= (CourseTypeMapper) ObjectFactory.getObject("CourseTypeMapper");
        List<CourseType> list=courseTypeMapper.selectFatherListById(id);
        return list;
    }

    @Override
    public void modifyCourseTypeById(String id, String typeName ,String parentId) throws NullSringException, TypeExistException {
        CourseTypeMapper courseTypeMapper= (CourseTypeMapper) ObjectFactory.getObject("CourseTypeMapper");
        if(ParameterUtil.isNull(typeName)){
            throw new NullSringException("数据为空无法修改");
        }
        CourseTypeExample example=new CourseTypeExample();
        example.or()
                .andParentIdEqualTo(Integer.parseInt(parentId))
                .andTypeNameEqualTo(typeName)
                .andIdNotEqualTo(Integer.parseInt(id));
        List<CourseType> list = courseTypeMapper.selectByExample(example);
        if(!list.isEmpty()){
            throw new TypeExistException("已存在同名课程，无法修改");
        }
        CourseType courseType=new CourseType();
        courseType.setTypeName(typeName);
        courseType.setId(Integer.parseInt(id));
        courseTypeMapper.updateByPrimaryKeySelective(courseType);
    }

    @Override
    public void yongCourseTypeById(String id,String status) throws Exception {
        CourseTypeMapper courseTypeMapper= (CourseTypeMapper) ObjectFactory.getObject("CourseTypeMapper");
        CourseType courseType = courseTypeMapper.selectByPrimaryKey(Integer.parseInt(id));
        CourseType TypeFather = courseTypeMapper.selectByPrimaryKey(courseType.getParentId());
        System.out.println("父"+TypeFather.getStatus());
        System.out.println("现在"+status);
        if(TypeFather.getStatus()==Integer.parseInt(CourseTypeConstant.COURSETYPE_STATUS_JINYONG)
                                        &&
            status.equals(CourseTypeConstant.COURSETYPE_STATUS_QIYONG)
        ){
            throw new FatherJinyongException("父课程类型已被禁用无法启用");
        }
        if(!(""+courseType.getStatus()).equals(status)){
            courseType.setStatus(Integer.parseInt(status));
            courseTypeMapper.updateByPrimaryKeySelective(courseType);


            //这里调用了课程中的禁用或启用方法，以保证课程类型禁用后该类型下的所用课程都被禁用
            CourseMapper courseMapper = (CourseMapper) ObjectFactory.getObject("CourseMapper");
            CourseService courseService = (CourseService) ObjectFactory.getObject("CourseServiceTarget");
            CourseExample courseExample =new CourseExample();
            courseExample.or()
                    .andCourseTypeIdEqualTo(Integer.parseInt(id));
            List<Course> courses = courseMapper.selectByExample(courseExample);
            if(!courses.isEmpty()){
                for (Course course:courses) {
                    System.out.println(courseService);
                    courseService.yongCourseById(""+course.getId(),status);
                }
            }
            //这里调用了课程中的禁用或启用方法，以保证课程类型启用后该类型下所用课程都被启用


        }

        CourseTypeExample example =new CourseTypeExample();
        example.or()
                .andParentIdEqualTo(Integer.parseInt(id));
        List<CourseType> list = courseTypeMapper.selectByExample(example);
        if(list.isEmpty()){
            return;
        }else {
            for (CourseType c:list) {
                yongCourseTypeById((""+c.getId()),(status));
            }
        }

    }
}
